package com.ruoyi.web.controller.ccc3gc;

import com.ccc3gc.mysystem.VO.OrderVO;
import com.ccc3gc.mysystem.VO.SearchParamVO;
import com.ccc3gc.mysystem.bean.Order;
import com.ccc3gc.mysystem.bean.OrderState;
import com.ccc3gc.mysystem.bean.OrderType;
import com.ccc3gc.mysystem.service.OrderService;
import com.ccc3gc.mysystem.service.OrderStateService;
import com.ccc3gc.mysystem.service.OrderTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController {
    @Autowired
    public OrderService orderService;
    @Autowired
    public OrderStateService orderStateService;
    @Autowired
    public OrderTypeService orderTypeService;
    //订单列表
    @GetMapping("/list")
    public TableDataInfo list(Order order){
        startPage();
        List<OrderVO> list = orderService.selectOrderList(order);
        System.out.println(list);
        return getDataTable(list);
    }

    //查询用户端订单列表，以及配送员大厅列表
    @PostMapping("/listUser")
    public AjaxResult selectOrderListUser(@RequestBody SearchParamVO searchParamVO){
        Integer userId = searchParamVO.getUserId();
        if(userId == null) {
            searchParamVO.setStateId(1);//如果是配送员查询则可以查询全部待接单订单
        }
        List<Order> orders = orderService.selectOrderListUser(searchParamVO);
        return AjaxResult.success(orders);
    }

    //通过订单Id查询订单详情
    @GetMapping("/query/{orderId}")
    public AjaxResult selectOrderByOrderId(@PathVariable int orderId){
        OrderVO orderVO = orderService.selectOrderByOrderId(orderId);
        AjaxResult ajax = AjaxResult.success();
        List<OrderState> orderStates = orderStateService.selectOrderState();
        List<OrderType> orderTypes = orderTypeService.selectOrderType();
        ajax.put("orderStates",orderStates);
        ajax.put("orderTypes",orderTypes);
        ajax.put(AjaxResult.DATA_TAG,orderVO);
        return ajax;
    }

    //添加订单
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody Order order){
        order.setStateId(1);//默认添加订单就是待取件状态
        String code = UUID.randomUUID().toString().replaceAll("[^0-9]","").substring(0, 6);
        order.setOrderNumber(LocalDate.now().toString().replace("-","")+code);
        int rows = orderService.instertOrder(order);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //修改订单信息
    @PostMapping("/updateOrder")
    public AjaxResult updateStaff(@RequestBody Order order){
        int rows = orderService.updateOrder(order);
        if(rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //根据Id删除订单信息
    @DeleteMapping("/{orderId}")
    public AjaxResult deleteOrder(@PathVariable int orderId){
        int rows = orderService.deleteOrderById(orderId);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    //批量删除
    @DeleteMapping("/deleteOrderBatch/{orderIds}")
    public AjaxResult deleteOrderBatch(@PathVariable List<Integer> orderIds){
        int rows = orderService.deleteOrderByIds(orderIds);
        if (rows>0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
